/**
 * 
 */
package com.yangtao.prototype.system.organize.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.prototype.system.dao.LogNoteDAO;
import com.yangtao.prototype.system.dao.OrganizeDAO;
import com.yangtao.prototype.system.entity.LogNote;
import com.yangtao.prototype.system.entity.Organize;
import com.yangtao.prototype.system.organize.service.OrganizeModel;
import com.yangtao.prototype.system.organize.service.OrganizeService;
import com.yangtao.prototype.util.CheckBoxTree;

/**
 * @author 杨涛
 * 
 */
@Service
public class OrganizeServiceImpl extends BaseService implements OrganizeService {
	@Autowired
	private OrganizeDAO organizeDAO;
	private LogNoteDAO ld;
	@Override
	public void save(OrganizeModel model) {
		Organize organize = model.getDomain();
		if (organize.getSupOrganize() != null) {
			organize.setSupOrganize(organizeDAO.load(organize
					.getSupOrganize().getId()));
		}
		organizeDAO.saveOrUpdate(organize);
		model.addInfo("save.success");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void list(OrganizeModel model) {
		organizeDAO.findPageByExample(model.getPage(), model.getDomain());
		for (Organize o : (List<Organize>) model.getPage().getItems()) {
			o.getSupOrganize();
			// if(o.getSubOrganize()== null){
			o.setSubOrganize(null);
			// }
		}
	}

	@Override
	public void findAll(OrganizeModel model) {
		List<Organize> all = organizeDAO.findTop();
		List<CheckBoxTree> tree = new ArrayList<CheckBoxTree>();
		for (Organize o : all) {
			tree.add(CheckBoxTree.loadFromOrganize(o));
		}
		model.setTree(tree);
	}

	@Override
	public void load(OrganizeModel model) {
		if (model.getDomain() != null
				&& StringHelper.isNotEmpty(model.getDomain().getId())) {
			model.setDomain(organizeDAO.load(model.getDomain().getId()));
		}
	}

	@Override
	public void delete(OrganizeModel model) {
		organizeDAO.delete(model.getDomain());
		model.addInfo("delete.success");
	}

	@Override
	public void setSS(OrganizeModel model) {
		// TODO Auto-generated method stub
		Organize org = organizeDAO.load(model.getDomain().getId());
		if(!org.hasChildren()){
			LogNote ln = new LogNote();
			ln.createOrgLog(org);
			ld.save(ln);
		}
	}

	@Override
	public void loadSelect(OrganizeModel model) {
		// TODO Auto-generated method stub
		
	}

}
